from typing import Any, Dict, List, Optional, Tuple, Callable, Awaitable

from ..managers.kafka_manager import KAFKA_MANAGER


async def kafka_send_json(topic: str, obj: Any, key: Optional[str] = None, headers: Optional[Dict[str, str]] = None) -> None:
    """Service wrapper to send JSON payload to Kafka topic."""
    await KAFKA_MANAGER.send_json(topic, obj, key=key, headers=headers)


async def kafka_send_bytes(topic: str, value: bytes, key: Optional[bytes] = None, headers: Optional[List[Tuple[str, bytes]]] = None) -> None:
    """Service wrapper to send bytes payload to Kafka topic."""
    await KAFKA_MANAGER.send_bytes(topic, value, key=key, headers=headers)


def kafka_consumer_task(topics: List[str], group_id: str, handler: Callable[[Any], Awaitable[None]], decode_json: bool = True):
    """Create a background consumer task; returns an async function schedule-able via BackgroundTasks."""
    return KAFKA_MANAGER.create_consumer_task(topics, group_id, handler, decode_json=decode_json)


async def kafka_stop_consumer(group_id: str, topics: List[str]) -> bool:
    """Stop a running consumer by group and topics."""
    return await KAFKA_MANAGER.stop_consumer(group_id, topics)


# Binding-based helpers
async def kafka_send_by_binding_json(binding_name: str, obj: Any, key: Optional[str] = None, headers: Optional[Dict[str, str]] = None) -> None:
    await KAFKA_MANAGER.send_by_binding_json(binding_name, obj, key=key, headers=headers)


async def kafka_send_by_binding_bytes(binding_name: str, value: bytes, key: Optional[bytes] = None, headers: Optional[List[Tuple[str, bytes]]] = None) -> None:
    await KAFKA_MANAGER.send_by_binding_bytes(binding_name, value, key=key, headers=headers)


def kafka_consumer_task_by_binding(binding_name: str, handler: Callable[[Any], Awaitable[None]]):
    return KAFKA_MANAGER.consumer_task_by_binding(binding_name, handler)